<html>
<head><meta charset="utf-8"><title>How to use the `rustc` crate in a plugin · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html">How to use the `rustc` crate in a plugin</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224369142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224369142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224369142">(Jan 28 2021 at 18:15)</a>:</h4>
<p>hey, I was trying to make a compiler plugin, I have <code>plugin = true</code> in my Cargo.toml, and the rustc_private feature, but when I <code>extern crate rustc</code>, I get "found staticlib <code>rustc</code> instead of rlib or dylib"</p>



<a name="224369248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224369248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224369248">(Jan 28 2021 at 18:16)</a>:</h4>
<p>I tried setting my crate-type to a bunch of things, nothing worked. I'm new to working with the compiler, if you'd even call making a plugin that.</p>



<a name="224369631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224369631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224369631">(Jan 28 2021 at 18:19)</a>:</h4>
<p>Rustc plugins are deprecated and will be removed in the future. I believe the only blocker for removing them currently is the gc safety checker in <a href="https://github.com/servo/servo">servo</a>.</p>



<a name="224369681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224369681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224369681">(Jan 28 2021 at 18:19)</a>:</h4>
<p>What do you want to use it for? You may be able to use a custom rustc driver instead.</p>



<a name="224369975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224369975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224369975">(Jan 28 2021 at 18:21)</a>:</h4>
<p>right. I'm trying to do a sort of hooking system at compile time. So you'd annotate a function with <code>#[hook(module::somefunc)]</code> and then <code>module::somefunc</code> would first call the annotated function, then proceed execution normally. I could do this with something like the detour crate but I want to do it at compiletime so many hooks can be added and be subject to inlining or other optimizations.</p>



<a name="224370057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224370057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224370057">(Jan 28 2021 at 18:22)</a>:</h4>
<p>You should use the stable proc macros instead.</p>



<a name="224370092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224370092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224370092">(Jan 28 2021 at 18:22)</a>:</h4>
<p>how would I do something like this with proc macros?</p>



<a name="224370190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224370190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224370190">(Jan 28 2021 at 18:23)</a>:</h4>
<p>unless im missing something, the only way i see of doing it would require modifying the target function's AST and adding a call, or something like that</p>



<a name="224370232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224370232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224370232">(Jan 28 2021 at 18:23)</a>:</h4>
<p>or the MIR or something</p>



<a name="224370481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224370481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224370481">(Jan 28 2021 at 18:25)</a>:</h4>
<p>First set the crate type to <code>proc-macro</code> inside <code>Cargo.toml</code></p>
<div class="codehilite" data-code-language="TOML"><pre><span></span><code><span class="k">[lib]</span>
<span class="n">crate_type</span> <span class="o">=</span> <span class="k">["proc-macro"]</span>
</code></pre></div>
<p>Then define a function like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[proc_macro_attribute]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">hook</span><span class="p">(</span><span class="n">args</span>: <span class="nc">TokenStream</span><span class="p">,</span><span class="w"> </span><span class="n">input</span>: <span class="nc">TokenStream</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">TokenStream</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// ...</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>You can then use the <code>syn</code> crate to parse the input and <code>quote</code> crate to create the output token stream.</p>



<a name="224370516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224370516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224370516">(Jan 28 2021 at 18:25)</a>:</h4>
<p>Proc macros work on the AST level.</p>



<a name="224370603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224370603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224370603">(Jan 28 2021 at 18:26)</a>:</h4>
<p>yeah, but this case requires modifying the AST of another function</p>



<a name="224370938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224370938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224370938">(Jan 28 2021 at 18:28)</a>:</h4>
<p>Oh, proc macros won't support that directly. At most you could have an attribute that is applied to all modules that searches for all <code>#[hook]</code> attributes and changes the hooked functions as necessary.</p>



<a name="224370981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224370981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224370981">(Jan 28 2021 at 18:29)</a>:</h4>
<p>That would require the hooking and hooked function to be in the same module though.</p>



<a name="224371563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224371563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224371563">(Jan 28 2021 at 18:32)</a>:</h4>
<p>yeah, I have them in different modules in this case. essentially what I'm trying to do is have plugins for my crate, and the plugins can hook functions in the crate. The plugins would just be put into a module with the proper name into the crate. I am using a custom program to add in the plugins, so technically I could scan the whole crate for functions, scan all the plugins for hooks, and do it that way, but that would be pretty complex and i'm not sure how I'd go about it.</p>



<a name="224371836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224371836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224371836">(Jan 28 2021 at 18:34)</a>:</h4>
<p><code>#![my_proc_macro]</code> is not supported at the crate level, so running a proc macros on the whole crate won't work.</p>



<a name="224371860"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224371860" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224371860">(Jan 28 2021 at 18:34)</a>:</h4>
<p>I could process it manually though</p>



<a name="224371876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224371876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224371876">(Jan 28 2021 at 18:34)</a>:</h4>
<p>using <code>syn</code>in my custom program</p>



<a name="224371898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224371898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224371898">(Jan 28 2021 at 18:35)</a>:</h4>
<p>That is possible.</p>



<a name="224371933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224371933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224371933">(Jan 28 2021 at 18:35)</a>:</h4>
<p>but I'd essentially have to reimplement rust's module system</p>



<a name="224371959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224371959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224371959">(Jan 28 2021 at 18:35)</a>:</h4>
<p>actually, I could just scan all files</p>



<a name="224372007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224372007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224372007">(Jan 28 2021 at 18:35)</a>:</h4>
<p>but that wouldn't give me much info about where the function actually is so when it's hooked I wouldn't know where to find it</p>



<a name="224372112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224372112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224372112">(Jan 28 2021 at 18:36)</a>:</h4>
<p>is there a place rust implements module resolution so I could use that as a reference?</p>



<a name="224372444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224372444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224372444">(Jan 28 2021 at 18:39)</a>:</h4>
<p>I think <a href="https://github.com/rust-lang/rust/blob/bf193d69fe48f4ec3cac57ab7abb4e9f2a291661/compiler/rustc_expand/src/module.rs">https://github.com/rust-lang/rust/blob/bf193d69fe48f4ec3cac57ab7abb4e9f2a291661/compiler/rustc_expand/src/module.rs</a></p>



<a name="224372672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224372672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> firecubez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224372672">(Jan 28 2021 at 18:40)</a>:</h4>
<p>alright</p>



<a name="224403426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224403426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224403426">(Jan 28 2021 at 22:26)</a>:</h4>
<p>there are also existing crates that do some magic which could allow what you want: <a href="https://docs.rs/inventory/0.1.10/inventory">https://docs.rs/inventory/0.1.10/inventory</a> and the underlying <a href="https://docs.rs/ctor/0.1.18/ctor">https://docs.rs/ctor/0.1.18/ctor</a></p>



<a name="224465066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20to%20use%20the%20%60rustc%60%20crate%20in%20a%20plugin/near/224465066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20to.20use.20the.20.60rustc.60.20crate.20in.20a.20plugin.html#224465066">(Jan 29 2021 at 12:47)</a>:</h4>
<p>Oh, that one might help too: <a href="https://docs.rs/linkme/0.1.1/linkme/">https://docs.rs/linkme/0.1.1/linkme/</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>